package org.cyhz.standardize.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.cyhz.domain.AutoInfoVO;
import org.cyhz.domain.SbVO;
import org.cyhz.domain.SsVO;
import org.cyhz.util.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service("ultimateCheck")
public class UltimateCheckImp implements UltimateCheck {

	@Value("#{configSettings['other.brand']}")
	private String otherBrand;

	@Value("#{configSettings['other.series']}")
	private String otherSeries;

	@Override
	public boolean isNb1InTitle(String title, List<String> nbs) {
		return isInTitle(title, nbs);
	}

	@Override
	public boolean isNs1InTitle(String title, List<String> nss) {
		return isInTitle(title, nss);
	}

	private final static boolean isInTitle(String t, List<String> multi) {

		boolean r = false;

		for (Iterator<String> mi = multi.iterator(); mi.hasNext();) {
			String m = (String) mi.next();
			if (t.contains(m)) {
				r = true;
				break;
			}
		}
		return r;
	}

	@Override
	public StandardizeResult check(StandardizeResult sr, AutoInfoVO autoInfoVO,
			String t3) {

		StringBuilder step = new StringBuilder();

		if (otherBrand.equals(sr.getSb())) {
			// sb is other
			step.append("sb is other,return obos");
			sr.setSs(otherSeries);
			Log.info(step.toString());
			return sr;
		} else {
			// sb is not other
			step.append("sb is not other;");
			if (otherSeries.equals(sr.getSs())) {
				// sb is not other , ss is other
				step.append("ss is other;");

				List<String> nbs = getNbListBySb(sr.getSb());
				if (isNb1InTitle(t3, nbs)) {
					// NB1 in t3
					step.append("NB1 in t3");
					Log.info(step.toString());
					return sr;
				} else {
					// NB1 not in t3
					step.append("sb not in t3");
					sr.setSb(otherBrand);
					Log.info(step.toString());
					return sr;
				}

			} else {
				// sb is not other , ss is not other
				step.append("ss is not other;");

				List<String> nbs = getNbListBySb(sr.getSb());
				if (isNb1InTitle(t3, nbs)) {
					// NB1 in t3
					step.append("NB1 in t3;");
					Log.info(step.toString());
					return sr;
				} else {
					// NB1 not in t3
					step.append("NB1 not in t3");
					List<String> nss = getNsListBySs(sr.getSs());
					if (isNs1InTitle(t3, nss)) {
						// NS1 in t3
						step.append("NS1 in t3");
						Log.info(step.toString());
						return sr;
					} else {
						// NS1 not in t3
						step.append("NS1 not in t3;");
						Log.info(step.toString());
						sr.setSb(otherBrand);
						sr.setSs(otherSeries);
						return sr;
					}

				}

			}

		}
	}

	private List<String> getNsListBySs(String ss) {

		List<SsVO> ssvos = new ArrayList<>();

		if (StandardizeResources.ssName_ssvoMapping.containsKey(ss)) {
			ssvos.add(StandardizeResources.ssName_ssvoMapping.get(ss));
		}

		List<String> nss = new ArrayList<>();
		for (SsVO ssvo : ssvos) {
			if (StandardizeResources.ssnsMapping.containsKey(ssvo)) {
				nss.addAll(StandardizeResources.ssnsMapping.get(ssvo));
			}
		}
		Map<String, String> m = new HashMap<>();
		for (String ns : nss) {
			m.put(ns, ns);
		}
		return new ArrayList<>(m.keySet());

	}

	private List<String> getNbListBySb(String sb) {
		List<SbVO> sbvos = new ArrayList<>();
		if (StandardizeResources.sbName_sbvoMapping.containsKey(sb)) {
			SbVO sv = StandardizeResources.sbName_sbvoMapping.get(sb);
			sbvos.add(sv);
		}
		List<String> nbs = new ArrayList<>();
		for (SbVO sbvo : sbvos) {
			if (StandardizeResources.sbnbMapping.containsKey(sbvo)) {
				nbs.addAll(StandardizeResources.sbnbMapping.get(sbvo));
			}
		}
		Map<String, String> m = new HashMap<>();
		for (String nb : nbs) {
			m.put(nb, nb);
		}
		return new ArrayList<>(m.keySet());
	}

}
